<?php

require_once "email/spEmailInterface.php";

/*
	currently we only support smtp
*/

class spEmailPhpMail implements spEmailInterface 
{
	private $mail;
	
	public function __construct( $param=array() )
	{
		$this->mail = new PHPMailer;
		$this->mail->CharSet = 'UTF-8';
		$this->mail->IsSMTP();									// Set mailer to use SMTP
		$this->mail->IsHTML( true ); 
		$this->mail->Host 		= $param["host"];				// Specify main and backup server
		$this->mail->Port 		= $param["port"];				// Port
		$this->mail->Username 	= $param["username"];			// SMTP username
		$this->mail->Password 	= $param["password"];			// SMTP password
		$this->mail->SMTPSecure	= 'ssl';						// Enable encryption, 'ssl' also accepted
		$this->mail->SMTPAuth 	= true;							// Enable SMTP authentication
		$this->mail->WordWrap	= 50;
		
		$this->mail->From 		= $param["from"];
		$this->mail->FromName 	= $param["from_name"];
		
		if ( isset($param["addresses"]) )
		{
			foreach ( $param["addresses"] as $email => $name )
			{
				$this->mail->AddAddress( $email, $name );  			// Add a recipient
			}
		}

		foreach ( $param["reply_to"] as $email => $name )
		{
			$this->mail->AddReplyTo( $email, $name );
		}

		if ( isset($param["cc"]) )
		{
			foreach ( $param["cc"] as $email => $name )
			{
				$this->mail->AddCC( $email, $name );
			}
		}

		if ( isset($param["bcc"]) )
		{
			foreach ( $param["bcc"] as $email => $name )
			{
				$this->mail->AddBCC( $email, $name );
			}
		}

		if ( isset($param["bcc_emailonly"]) )
		{
			foreach ( $param["bcc_emailonly"] as $email )
			{
				$this->mail->AddBCC( $email );
			}
		}
		
		$this->mail->Subject = $param["subject"];
		
		$body = $param["body"];
		if ( isset($param["create_html_body"]) && ($param["create_html_body"]=="true" || $param["create_html_body"]==true) )
		{
			$body = "
				<html>
					<head>
						<title>Tumbling Team Fyn</title>
					</head>
					<body>
					".stripslashes($body)."
					</body>
				</html>
			";
		}
		
		$this->mail->Body  = $body;
	}
	
	public function getErrorInfo()
	{
		return $this->mail->ErrorInfo;
	}
	
	public function send()
	{
		return $this->mail->Send();
	}
}

?>